package jk.fox.node.rule;

import jk.fox.node.node.RpcNode;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @Auther cuichao
 * @Description: RandomRule
 * @Date: create in 2020/4/20 14:18
 */
public class RandomRule extends SingleRule {
    @Override
    public String rule(RpcNode node) {
        String rule = super.rule(node);
        if(rule != null){
            return rule;
        }
        int limit = node.getUriList().size();
        int position = chooseRandomInt(limit);
        return node.getUriList().get(position);
    }

    @Override
    public String getName() {
        return "random";
    }


    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }
}
